﻿jQuery.fn.extend({
    //根据浏览器窗口大小变化自动调整对象height与width
    autoResize: function (settingOptions) {
        var self = this;
        this.settings = {
            reduceWidth: 0,
            reduceHeight: 0,
            minWidth: 0,
            maxWidth: 0,
            minHeight: 0,
            maxHeight: 0,
            orientationMode: "all", //all、height、width 
            referToExpr: window //参照对象jquery expr
        }

        $.extend(true, this.settings, settingOptions || {});

        this.referToControl = $(this.settings.referToExpr);
        if (this.referToControl.length == 0 || this.length == 0) {
            return;
        }

        $(window).resize(function () {

            self.each(function () {
                var width = self.referToControl.width() - self.settings.reduceWidth - $(this).offsetAllWidth() + $(this).width();
                var height = self.referToControl.height() - self.settings.reduceHeight - $(this).offsetAllHeight() + $(this).height();

                if (this.length == 0)
                    return;

                if (self.settings.orientationMode == 'all' || self.settings.orientationMode == 'width') {
                    if ($(this).css("overflow-y") == "scroll") {
                        width = width - 20;
                    }

                    if (self.settings.minWidth != 0 && self.settings.minWidth > width) {
                        width = self.settings.minWidth;
                    }

                    if (self.settings.maxWidth != 0 && self.settings.maxWidth < width) {
                        width = self.settings.maxWidth;
                    }

                    $(this).width(width);
                }

                if (self.settings.orientationMode == 'all' || self.settings.orientationMode == 'height') {
                    if ($(this).css("overflow-x") == "scroll") {
                        height = height - 20;
                    }

                    if (self.settings.minHeight != 0 && self.settings.minHeight > height) {
                        height = self.settings.minHeight;
                    }

                    if (self.settings.maxHeight != 0 && self.settings.maxHeight < height) {
                        height = self.settings.maxHeight;
                    }

                    $(this).height(height);
                }
            });
        });

        $(window).resize();
    },

    //对象高度 + 边框 + 内补白 + 外补白
    offsetAllHeight: function () {
        var offsetHeight = this[0].offsetHeight;
        if (!offsetHeight) {
            offsetHeight = this.height();
        }

        return offsetHeight + ue.framework.convert.toInt32(this.css("margin-top")) + ue.framework.convert.toInt32(this.css("margin-bottom"));
    },

    //对象宽度 宽度 + 边框 + 内补白 + 外补白 
    offsetAllWidth: function () {
        var offsetWidth = this[0].offsetWidth;
        if (!offsetWidth) {
            offsetWidth = this.width();
        }
        return this[0].offsetWidth + ue.framework.convert.toInt32(this.css("margin-left")) + ue.framework.convert.toInt32(this.css("margin-right"));
    },

    //输入表格
    inputTable: function (className) {
        this.each(function () {
            var cells = parseInt($(this).attr("cells"))
            if (!cells) cells = 2;
            $(this).find(" > tbody > tr > td:not([isTitle=false],[class=btnConfirm])").each(function (i) {
                if (i % cells == 0) $(this).addClass("title");
            });

            $(this).find("td.btnConfirm").attr("colspan", $(this).find("tr:first > td").length);
        });

        if (!className) {
            className = "inputTable";
        }

        this.addClass(className);
    },

    dataGrid: function (settingOptions) {
        var self = this;
        this.settings = {
            Class: "dataTable",
            SeparatorTemplateClass: "separator",
            HoverClass: "hover",
            gridExpr: ">tbody>tr"
        }

        $.extend(true, this.settings, settingOptions || {});

        self.addClass(self.settings.Class);

        var grid = self.find(self.settings.gridExpr);
        if (grid.length == 0) {
            grid = self.find(">tr");
        }

        if (grid && grid.length > 0) {
            grid.filter(":odd").addClass(self.settings.SeparatorTemplateClass);
            grid.hover(function () {
                $(this).addClass(self.settings.HoverClass);
            }, function () {
                $(this).removeClass(self.settings.HoverClass);
            });

            grid.each(function () {
                var href = $(this).attr("href");

                if (href && href.length > 0) {
                    $(this).css("cursor", "pointer").click(function () {
                        ue.framework.response.redirect(href);
                    }).find("td > a,td>input").click(function (e) {
                        //阻止事件冒泡
                        if (e && e.stopPropagation) {
                            //W3C取消冒泡事件
                            e.stopPropagation();
                        } else {
                            //IE取消冒泡事件
                            window.event.cancelBubble = true;
                        }
                    });
                }
            });
        }
    },

    //加入收藏夹
    addFavorite: function (url, title) {
        this.click(function () {
            if (document.all) {
                window.external.AddFavorite(url, title);
            }
            else if (window.sidebar) {
                window.sidebar.addPanel(title, url, "");
            }
        });
    },

    //js时钟
    clock: function () {
        var self = this;
        var stepFun = function (format) {
            var Timer = new Date();
            var minutes = Timer.getMinutes();
            var seconds = Timer.getSeconds();

            if (minutes < 10)
                minutes = "0{0}".Format(minutes);

            if (seconds < 10)
                seconds = "0{0}".Format(seconds);

            if (!format) {
                format = "{0}年{1}月{2}日 {3}:{4}:{5} 星期{6}";
            }

            self.text(format.Format(Timer.getFullYear(), Timer.getMonth() + 1, Timer.getDate(), Timer.getHours(), minutes, seconds, ["日", "一", "二", "三", "四", "五", "六"][Timer.getDay()]));

            setTimeout(stepFun, 1000);
        }
        stepFun();
    },

    //全选，反选
    allChecked: function (otherCheckExpr, otherIsEnabled) {
        this.each(function () {
            $(otherCheckExpr).attr("checked", $(this).attr("checked") == "checked");

            $(this).change(function () {
                var checked = $(this).attr("checked") == "checked";
                $(otherCheckExpr).attr("checked", checked);
                if (otherIsEnabled) {
                    $(otherCheckExpr).attr("disabled", checked);
                }
            }).change();
        });
    },

    //智能浮动框
    smartFloat: function () {
        var position = function (element) {
            //占位符，当element设置position时，需要让占位符替代element显示在element的原位置上，要不然页面element原位置因缺少element而乱掉
            var placeholder = null,
                position = element.css("position"),
                width = element.css('width');

            $(window).scroll(function () {
                var top = (placeholder || element).position().top,
                    scrollTop = $(this).scrollTop();

                if (!placeholder && scrollTop > top) {
                    placeholder = element.clone().insertAfter(element).show();

                    element.css({
                        position: "fixed",
                        top: 0,
                        width: width,
                        'z-index': 99
                    });
                }
                else if (placeholder && scrollTop < top) {
                    element.css('position', position);
                    placeholder.hide();
                    placeholder = null;
                }
            });
        };
        return this.each(function () {
            position($(this));
        });
    },

    //设置输入框默认显示文字，当鼠标焦点时文字删除，离开焦点时如果为空，重新显示提示信息
    textBoxTip: function (settingOptions) {
        var settings = {
            Title: "请输入",
            TipClass: "tipClass",
            TipStyle: { color: "#999" },
            SubmitExp: "#btnConfirm,#btnSearch,#btnAdd,#btnEdit"
        };

        $.extend(true, settings, settingOptions || {});

        var getDefaultValue = function (element) {
            var label = element.attr("title");
            return label ? label : settings.Title;
        }

        var setClass = function (element) {
            element.addClass(settings.TipClass);
            element.css(settings.TipStyle);
        };

        var clearClass = function (element) {
            element.removeClass(settings.TipClass);
            element.removeAttr("style");
        };

        var valIsChanged = function (element) {
            return element.val() != getDefaultValue(element) && element.val().length > 0;
        };

        var tip = function (element) {

            element.focus(function () {
                if (!valIsChanged(element)) {
                    element.val("");
                    clearClass(element);
                }
            });

            element.focusout(function () {
                if (!valIsChanged(element)) {
                    element.val(getDefaultValue(element));
                    setClass(element);
                }
                else {
                    clearClass(element);
                }

            });

            element.focusout();

            $(settings.SubmitExp).click(function () {
                if (!valIsChanged(element)) {
                    element.val("");
                }
            });
        };

        return this.each(function () {
            tip($(this));
        });
    },

    //为按钮添加一个事件，当目标对象显示的时候，点击按钮隐藏目标对象。当目标对象显示的时候，点击按钮隐藏目标对象。
    toggleButton: function (targetExp, settingOptions) {
        var self = this;
        self.settings = {
            targetShowBtnClass: "btn_arrow_down",
            targetHideBtnClass: "btn_arrow_up",
            targetDefaultStatus: false
        };
        self.target = $(targetExp);

        $.extend(true, self.settings, settingOptions || {});

        self.toggleTarget = function (isShow) {
            if (isShow) {
                self.addClass(self.settings.targetShowBtnClass);
                self.removeClass(self.settings.targetHideBtnClass);
                self.target.show();
            }
            else {
                self.addClass(self.settings.targetHideBtnClass);
                self.removeClass(self.settings.targetShowBtnClass);
                self.target.hide();
            }
        };

        self.toggle(function () {
            self.toggleTarget(true);
        }, function () {
            self.toggleTarget(false);
        });

        if (this.settings.targetDefaultStatus) {
            self.click();
        }
        else {
            self.toggleTarget(false);
        }
    },

    //条件筛选器，多个a链接选择项，点击某个链接后，其它的链接消失，并显示选中的项
    conditionFilter: function (settingOptions) {
        var self = this;
        self.settings = {
            template: "<div><em>您已选择：<em><span>{0}<a class='close'></a></span></div>",
            closeOnClick: function (element) { },
            onClick: function (element) { }
        }
        self.itemArray = self.find("a");
        self.showFlat = false;

        $.extend(true, self.settings, settingOptions || {});

        self.itemArray.click(function () {
            self.showFlat = $(string.Format(self.settings.template, $(this).text()));
            self.showFlat.find("a.close").click(function () {
                if (self.settings.closeOnClick) {
                    self.settings.closeOnClick(this);
                }

                self.showFlat.remove();
                self.show();
            });

            if (self.settings.onClick) {
                self.settings.onClick(this);
            }

            self.after(self.showFlat);
            self.showFlat.show();
            self.hide();
        });
    },

    //异步加载的多级结构 弹出多项选择框
    multiSelector: function (settingOptions) {
        var self = this;
        self.settings = {
            title: "选择知识点",
            getChildrenUrl: "/Question/GetChildPointNodes",
            otherParam: {},
            selectedPanelExpr: "#pointSelectedPanel",
            selectedItemRemoveButtonExpr: "a.btn_cls",
            itemPanelExpr: "#pointSelectPanel ul",
            searchKeyInputExpr: "#searchPointNameInput",
            searchButtonExpr: "#searchPointButton",
            confirmButtonExpr: "#pointSelectorConfirm",
            cancelButtonExpr: "#pointSelectorCancel",
            template: "<div id='pointSelectedPanel' class='mt10'>你已经选择：</div>" +
                      "<div class='mt10 mb10'>" +
                           "<input id='searchPointNameInput' type='text' class='w_400 h_20 lh_20 vam mr10' />" +
                           "<a id='searchPointButton' class='btn_01'><span>搜索</span></a>" +
                      "</div>" +
                      "<dl id='pointSelectPanel' class='dl_lsit cf'><dd><ul></ul></dd><dd><ul></ul></dd><dd><ul></ul></dd><dd><ul></ul></dd></dl>" +
                      "<div class='tc pt15'><a id='pointSelectorConfirm' class='btn_yes'>确定</a><a id='pointSelectorCancel' class='btn_no'>取消</a></div>",
            selectedItemTemplate: "<span class='sel_bar'><a class='link_2' href='javascript:;'>{0}</a><a class='btn_cls'></a></span>",
            itemTemplate: "<li><a class='{1}'>{0}</a></li>",
            loadingTemplate: "<li>加载中…</li>",
            currentItemClass: "curr",
            parentItemClass: "next",
            maxPointNumber: 5,
            confirmCallback: function (data) { },
            show: function (title, content) { dlg.Com("pointSelectorPanel", title, content, 940, function () { dlg.cls("pointSelectorPanel") }) },
            close: function () { dlg.cls("pointSelectorPanel"); }
        };
        self.items = new Array();

        $.extend(true, self.settings, settingOptions || {});

        self.buildShowPanel = function () {
            var selectedPanel = $(self.settings.selectedPanelExpr);
            selectedPanel.find($(self.settings.selectedItemTemplate).get(0).tagName).remove();
            $.each(self.items, function (index, item) {
                selectedPanel.append($(string.Format(self.settings.selectedItemTemplate, item.name)));
            });
        };

        self.add = function (data) {
            if (self.items.length >= self.settings.maxPointNumber) {
                alert(string.Format("最多只能选择{0}个知识点。", self.settings.maxPointNumber));
                return;
            }

            if ($.inArray(data, self.items) == -1) {

                self.items.push(data);

                self.buildShowPanel();
            }
        };

        self.remove = function (data) {
            var index = $.inArray(data, self.selectedItems)
            if (index >= 0) {
                self.removeAt(index);
            }
        };
        self.removeAt = function (index) {

            if (index >= self.items.length)
                return;

            self.items.splice(index, 1);
            self.buildShowPanel();
        }

        self.loadingLock = function (content) {
            content.empty();
            content.append(self.settings.loadingTemplate);
        };

        self.loadedUnlock = function (content) {
            content.empty();
        };

        self.getChildren = function (currentId, level, searchKey) {

            if (!searchKey || level != 0) {
                searchKey = "";
            }

            var urlData = { pointCode: currentId, level: level, searchKey: searchKey };
            $.extend(true, urlData, self.settings.otherParam || {});

            $.get(self.settings.getChildrenUrl, urlData, function (data) {

                var itemPanelArray = $(self.settings.itemPanelExpr);
                var currentItemPanel = itemPanelArray.eq(level);
                if (level == 0) {
                    itemPanelArray.empty();
                }

                self.loadedUnlock(currentItemPanel);

                if (data && data.length > 0) {

                    $.each(data, function (index, itemData) {
                        var item = $(string.Format(self.settings.itemTemplate, itemData.name, itemData.isParent ? self.settings.parentItemClass : ""));

                        item.click(function () {

                            if (itemData.isParent) {
                                self.getChildren(itemData.id, level + 1);
                                itemPanelArray.filter(string.Format(":gt({0})", level)).empty();
                                self.loadingLock(itemPanelArray.eq(level + 1));
                            }
                            else {
                                self.add(itemData);
                            }

                            $(this).addClass(self.settings.currentItemClass).siblings().removeClass(self.settings.currentItemClass);
                        });

                        currentItemPanel.append(item);
                    });
                }
            });
        };

        self.init = function () {
            self.click(function () {
                self.getChildren("", 0);
                self.settings.show(self.settings.title, self.settings.template);
                self.loadingLock($(self.settings.itemPanelExpr).eq(0));
                self.buildShowPanel();
            });

            $(self.settings.selectedPanelExpr).find(self.settings.selectedItemRemoveButtonExpr).live("click", function () {
                self.removeAt($(self.settings.selectedPanelExpr).find(self.settings.selectedItemRemoveButtonExpr).index(this));
            });

            $(self.settings.cancelButtonExpr).live("click", function () {
                self.settings.close();
            });

            $(self.settings.confirmButtonExpr).live("click", function () {
                self.settings.confirmCallback(self.items);
                self.settings.close();
            });

            $(self.settings.searchButtonExpr).live("click", function () {
                self.loadingLock($(self.settings.itemPanelExpr).eq(0));
                self.getChildren("", 0, $(self.settings.searchKeyInputExpr).val());
            });
        };

        self.init();

        return self;
    },

    //倒计时
    countdown: function (settingOptions) {
        var self = this;
        self.settings = {
            showExpr: "span.countdown",
            timeSpan: "00:00:00",
            //倒计时结束回调
            endCallback: function (content) { }
        };
        $.extend(true, self.settings, settingOptions || {});

        self.Interval = null;
        self.countArray = $(self.settings.timeSpan.split(':')).map(function () { return parseInt(this); });
        self.maxCountArray = [999, 59, 59];

        if (self.countArray.length == 4) {
            self.countArray[1] = self.countArray[1] + self.countArray[0] * 24;
            self.countArray.splice(0, 1);
        }

        //显示当前计时
        self.showCount = function () {
            self.find(self.settings.showExpr).each(function (index) {
                if (self.countArray.length > index && index < 3) {
                    var count = Math.round(self.countArray[index]);
                    $(this).text(count > 9 ? count : "0" + count);
                }
            });
        };

        //跳秒
        self.removeStep = function (index) {
            if (self.countArray[index] > 0) {
                self.countArray[index] = self.countArray[index] - 1;
            }
            else {
                self.countArray[index] = self.maxCountArray[index];

                if (index > 0) {
                    self.removeStep(index - 1);
                }
            }
        };

        //判断是否结束，如果结束那么清空Interval并调用endCallback回调方法
        self.judgeEndHandler = function () {
            var greaterThanZeroArray = $.grep(self.countArray, function (val, itemIndex) { if (val > 0) return val; });
            if (greaterThanZeroArray == 0) {
                self.settings.endCallback(self);
                clearInterval(self.Interval);
                self.Interval = null;
            }
        }

        self.showCount();
        self.Interval = setInterval(function () {
            self.removeStep(2);
            self.showCount();
            self.judgeEndHandler();
        }, 1000);
    },
    //异步加载
    loading: function (settingOptions) {
        var self = this;
        self.settings = {
            url: "",
            data: {},
            cache: true,
            success: function (data) { },
            loadingHtml: "<img src='/Scripts/zTree/zTreeStyle/img/loading.gif' style='vertical-align:middle' /> 加载中...",
            errorHtml: "加载出错……"
        };
        $.extend(true, self.settings, settingOptions || {});

        self.isLoading = function (isLoading) {
            if (isLoading != null && isLoading != undefined) {
                self.attr("isLoading", isLoading)
            }
            else {
                isLoading = self.attr("isLoading");
            }

        };

        if (!self.isLoading() && self.settings.url) {

            $.ajax({
                url: self.settings.url,
                dataType: "json",
                data: self.settings.data,
                beforeSend: function (XMLHttpRequest) {
                    self.isLoading(true);
                    self.html(self.settings.loadingHtml);
                },
                dataFilter: function (data, type) {
                    self.empty();
                    return data;
                },
                complete: function (XMLHttpRequest, textStatus) {
                    setTimeout(function () { self.isLoading(false); }, 1000);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    self.html(self.settings.errorHtml);
                },
                success: self.settings.success
            });
        }
    }
});